package utils;

import java.io.IOException;
import java.util.Iterator;

import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.io.GraphMLFile;

public class GraphReader 
{
	@SuppressWarnings("unchecked")
	public static Integer[][] getGraph(String filename) throws IOException 
	{
		GraphMLFile gml = new GraphMLFile();
		Graph g = gml.load(filename);

		int size = g.getVertices().size();
		
		// initialisation
		Integer[][] graph = new Integer[size][size];
		for(int i = 0; i < graph.length; i++)
		{
			for(int j = 0; j < graph.length; j++)
			{
				graph[i][j] = null;
			}
		}
		
		for (Iterator<Object> iter = g.getVertices().iterator(); iter.hasNext();) 
		{
			Vertex v = (Vertex) iter.next();
			
			int name = Integer.parseInt(v.getUserDatum("name").toString());
			graph[name][name] = Integer.parseInt(v.getUserDatum("weight").toString());
		}
		
		for (Iterator<Object> iter = g.getEdges().iterator(); iter.hasNext();) 
		{
			Edge e = (Edge) iter.next();
			
			int source = Integer.parseInt(e.getUserDatum("source_name").toString());
			int target = Integer.parseInt(e.getUserDatum("target_name").toString());
			graph[source][target] = Integer.parseInt(e.getUserDatum("weight").toString());
		}
		
		return graph;
	}
}
